package com.itheima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.article.dto.ArticleBehaviourDtoQuery;
import com.itheima.article.dto.ArticleInfoDto;
import com.itheima.article.mapper.ApArticleConfigMapper;
import com.itheima.article.mapper.ApArticleContentMapper;
import com.itheima.article.mapper.ApArticleMapper;
import com.itheima.article.pojo.ApArticle;
import com.itheima.article.pojo.ApArticleConfig;
import com.itheima.article.pojo.ApArticleContent;
import com.itheima.article.service.ApArticleService;
import com.itheima.common.pojo.PageInfo;
import com.itheima.common.pojo.PageRequestDto;
import com.itheima.common.pojo.Result;
import com.itheima.user.feign.ApUserFollowFeign;
import com.itheima.user.pojo.ApUserFollow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author 作者
 * @since 2021-08-20
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApUserFollowFeign apUserFollowFeign;


    /***
     * 查询当前用户对当前文章操作的状态信息
     *  1:是否收藏
     *  2:是否已关注作者
     *  3:是否不喜欢
     *  4:是否点赞
     * @param dto
     * @return
     */
    @Override
    public Map<String, Object> loadArticleBehaviour(ArticleBehaviourDtoQuery dto) {
        //1:默认值   默认没有关注、没有点赞、没有点【不喜欢】、没有收藏.....
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("isfollow",false);    //默认没有关注该文章作者
        resultMap.put("islike",false);      //默认没有点赞
        resultMap.put("isunlike",false);    //默认没有点击不喜欢
        resultMap.put("iscollection",false); //默认没有收藏该文章

        //2:远程调用查询【是否关注该作者】、【是否点赞】、【有没有点击 不喜欢】、【有没有收藏】
        //2.1是否已关注
        Result<ApUserFollow> followResult = apUserFollowFeign.getByAuthorId(dto.getAuthorId());
        if(followResult.getData()!=null){
            //关注了
            resultMap.put("isfollow",true);
        }
        //3:更改默认值->将结果集同步到默认值中

        return resultMap;
    }



    /***
     * 文章详情
     *  1)ApArticle
     *  2)ApArticleConfig
     *  3)ApArticleContent
     */
    @Override
    public ArticleInfoDto detailByArticleId(Long id) {
        //1）根据ID查询ApArticle
        ApArticle apArticle = apArticleMapper.selectById(id);

        //2)根据ID查询ApArticleConfig
        QueryWrapper<ApArticleConfig> wrapper = new QueryWrapper<>();
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(wrapper);

        //3)根据ID查询ApArticleContent
        QueryWrapper<ApArticleContent> articleContentQueryWrapper = new QueryWrapper<ApArticleContent>();
        articleContentQueryWrapper.eq("article_id",id);
        ApArticleContent articleContent = apArticleContentMapper.selectOne(articleContentQueryWrapper);

        //4)封装成ArticleInfoDto
        ArticleInfoDto dto = new ArticleInfoDto(apArticle,articleContent,apArticleConfig);
        return dto;
    }


    /***
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public PageInfo<ApArticle> searchOrder(PageRequestDto<ApArticle> dto) {
        //1:查询总数
        Long total = apArticleMapper.findCount(dto.getBody().getChannelId());

        //2:分页查询集合信息
        List<ApArticle> articles = apArticleMapper.findList(dto);

        //3:封装PageInfo<T>
        Long totalPages = total%dto.getSize()==0? total/dto.getSize() : total/dto.getSize()+1;
        PageInfo<ApArticle> pageInfo = new PageInfo<ApArticle>(dto.getPage(),dto.getSize(),total,totalPages,articles);
        return pageInfo;
    }

    /***
     * 文章信息同步增加
     * @param dto
     */
    @Override
    public void saveArticleInfo(ArticleInfoDto dto) {
        //1:ApArticle增加
        apArticleMapper.insert(dto.getApArticle());

        //2:ApArticleContent增加
        dto.getApArticleContent().setArticleId(dto.getApArticle().getId());
        apArticleContentMapper.insert(dto.getApArticleContent());

        //3:ApArticleConfig增加
        dto.getApArticleConfig().setArticleId(dto.getApArticle().getId());
        apArticleConfigMapper.insert(dto.getApArticleConfig());
    }
}
